package com.sendi.mobilework;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sendi.mobilework.api.Api;
import com.sendi.mobilework.datamodel.Result;
import com.sendi.mobilework.global.GlobalContent;
import com.sendi.mobilework.util.HttpUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 提交申请工作流程
 */
public class AddApplyActivity extends AppCompatActivity implements View.OnClickListener {

    private String mDepartment;

    private TextView hr;//HR
    private TextView cw;//财务部
    private TextView js;//技术部
    private TextView rs;//人事部

    private EditText et_name;
    private EditText et_reason;

    private Api mApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_apply);
        Retrofit mRetrofit= HttpUtils.getRetrofit(GlobalContent.ADD_APPLY_SEVER);
        mApi=mRetrofit.create(Api.class);
        initView();
        initListener();
    }

    /**
     * 监听设置
     */
    private void initListener() {
        hr.setOnClickListener(this);
        cw.setOnClickListener(this);
        js.setOnClickListener(this);
        rs.setOnClickListener(this);
    }

    private void initView() {
        hr = (TextView) findViewById(R.id.department_hr);
        cw = (TextView) findViewById(R.id.department_cw);
        js = (TextView) findViewById(R.id.department_js);
        rs = (TextView) findViewById(R.id.department_rs);

        et_name= (EditText) findViewById(R.id.et_apply_name);
        et_reason= (EditText) findViewById(R.id.et_reason);
    }

    /**
     * 提交申请
     *
     * @param view
     */
    public void submit(View view) {
        String name=et_name.getText().toString();
        final String reason=et_reason.getText().toString();
        if (TextUtils.isEmpty(name)){
            Toast.makeText(this, "名字不能为空", Toast.LENGTH_SHORT).show();
        }
        else if (TextUtils.isEmpty(reason)){
            Toast.makeText(this, "原因不能为空", Toast.LENGTH_SHORT).show();
        }else if (TextUtils.isEmpty(mDepartment)){
            Toast.makeText(this, "请选择部门", Toast.LENGTH_SHORT).show();
        }else {
            //提交
            Log.i("time", "submit: "+getData());
            Log.i("name", "submit: "+name);
            Log.i("department", "submit: "+mDepartment);
            Log.i("content", "submit: "+reason);
            mApi.submitApply(name,mDepartment,getData(),reason).enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Response<Result> response) {
                    if (response.isSuccess()){
                        Toast.makeText(AddApplyActivity.this, ""+response.body().getResult(),Toast.LENGTH_SHORT).show();
                        Log.i("s", "onResponse: "+response.body().getResult());
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    Toast.makeText(AddApplyActivity.this, "网络开小差", Toast.LENGTH_SHORT).show();
                    Log.i("SENDI", "onFailure: "+t.getMessage()+t.getCause());

                }
            });
        }
    }

    private String getData(){
        //日期格式
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(new Date());
    }

    @Override
    public void onClick(View v) {
        reset();
        TextView tv = (TextView) v;
        tv.setBackgroundColor(Color.RED);
        tv.setTextColor(Color.WHITE);
        switch (v.getId()) {
            case R.id.department_hr:
                mDepartment = "HR";
                break;
            case R.id.department_cw:
                mDepartment = "财务部";
                break;
            case R.id.department_js:
                mDepartment = "技术部";
                break;
            case R.id.department_rs:
                mDepartment = "人事部";
                break;
            default:break;
        }
    }

    private void reset(){
        hr.setBackgroundColor(Color.WHITE);
        cw.setBackgroundColor(Color.WHITE);
        js.setBackgroundColor(Color.WHITE);
        rs.setBackgroundColor(Color.WHITE);

        hr.setTextColor(Color.RED);
        cw.setTextColor(Color.RED);
        js.setTextColor(Color.RED);
        rs.setTextColor(Color.RED);
    }

    public void back(View view) {
        this.finish();
    }
}
